package com.skyaims.biz.factory;

import com.skyaims.biz.exception.RegisterClassException;
import com.skyaims.biz.processor.MessageProcessor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by wujianing on 14-3-14.
 */
public class MessageProcessorFactory {

    public static final Logger log = LoggerFactory.getLogger(MessageProcessorFactory.class);

    private static Map<Integer, MessageProcessor> processors = new HashMap<Integer, MessageProcessor>();

    public static void register(Integer action, Class<? extends MessageProcessor> messageProcessor) throws Exception {

        synchronized (MessageProcessorFactory.class) {
            if (!processors.containsKey(action)) {
                processors.put(action, messageProcessor.newInstance());
                log.info("message action {} 's processor is {}", action, messageProcessor.getSimpleName());
            } else {
                throw new RegisterClassException("register class " + messageProcessor.getName() + " , action " + action + "faild , target already");
            }
        }

    }

    public static MessageProcessor getMessageProcessor(Integer action) {

        return processors.get(action);

    }

}
